#define layout()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vòng quay may mắn</title>
    <link rel="stylesheet" href="../wx/choujianng/award/css/style.css?v=4">
</head>
<link href="../wx/choujianng/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../wx/choujianng/js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="../wx/choujianng/js/awardRotate.js"></script>
<script type="text/javascript">
	
var turnplate={
		restaraunts:[],				//大转盘奖品名称
		colors:[],					//大转盘奖品区块对应背景颜色
		outsideRadius:192,			//大转盘外圆的半径
		textRadius:155,				//大转盘奖品位置距离圆心的距离
		insideRadius:68,			//大转盘内圆的半径
		startAngle:0,				//开始角度
		
		bRotate:false				//false:停止;ture:旋转
};
var nums = #(CJCS);

$(document).ready(function(){
	#for(x:PRIZELIST)
	      turnplate.restaraunts[#(for.index)] ="#(x.prize_name)";
	#end
	turnplate.colors = ["#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF"];
	
//	turnplate.colors = ["#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF"];
	var rotateTimeOut = function (){
		$('#wheelcanvas').rotate({
			angle:0,
			animateTo:2160,
			duration:8000,
			callback:function (){
				alert('网络超时，请检查您的网络设置！');
			}
		});
	};
	//旋转转盘 item:奖品位置; txt：提示语;
	var rotateFn = function (item, txt, falg){
		var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length*2));
		if(angles<270){
			angles = 270 - angles; 
		}else{
			angles = 360 - angles + 270;
		}
		$('#wheelcanvas').stopRotate();
		$('#wheelcanvas').rotate({
			angle:0,
			animateTo:angles+1800,
			duration:8000,
			callback:function (){
				 //0:成功 1：奖品已发完
				 if(falg == 0){
					 $('.article').show();
				 	 $('.awards').html(txt);
				 }else if(falg == 2){
				 	alert("谢谢参与，下次努力噢~喵喵喵~`");
				 }else{
				 	alert(txt);
				 }
				 flag = 1;
				turnplate.bRotate = !turnplate.bRotate;
			}
		});
	};
	var flag = 1;
	$('.pointer').click(function (){
	   var item = -1;
	   if(flag == 1&&nums>0){
	   	    flag =0;
	   	    $.ajax({
             type: "GET",
             url: "../marketingController/getAwards",
             data: {USER_CODE:#(USER.USER_CODE), PARTY_ID:#(PARTY_ID)},
             dataType: "json",
             async: false ,
             success: function(data){
             	 nums = nums -1;
             	 $('.nums').text(nums);
             if(data.msg == "SUCCESS"){
             	 item = turnplate.restaraunts.indexOf(data.data.prize_name)+1;
             	 if(turnplate.bRotate)return;
             	 
				turnplate.bRotate = !turnplate.bRotate;
				rotateFn(item, turnplate.restaraunts[item-1],0);
             } else if(data.data!=null){
             	 item = turnplate.restaraunts.indexOf(data.data.prize_name)+1;
             	 if(turnplate.bRotate)return;
				 turnplate.bRotate = !turnplate.bRotate;
				 if(data.data.prize_name=='谢谢参与'){
				 	 rotateFn(item, data.msg,2);
				 }else{
				 	 rotateFn(item, data.msg,1);
				 }
				
             }else{
             	alert(data.msg);
             }
			 }
         });
	   }
	  
	});
});

function rnd(n, m){
	var random = Math.floor(Math.random()*(m-n+1)+n);
	return random;
}
//页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染
window.onload=function(){
	drawRouletteWheel();
};
function drawRouletteWheel() {    
  var canvas = document.getElementById("wheelcanvas");    
  if (canvas.getContext) {
	  //根据奖品个数计算圆周角度
	  var arc = Math.PI / (turnplate.restaraunts.length/2);
	  var ctx = canvas.getContext("2d");
	  //在给定矩形内清空一个矩形
	  ctx.clearRect(0,0,422,422);
	  //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式  
	  ctx.strokeStyle = "#FFBE04";
	  //font 属性设置或返回画布上文本内容的当前字体属性
	  ctx.font = '16px Microsoft YaHei';      
	  for(var i = 0; i < turnplate.restaraunts.length; i++) {       
		  var angle = turnplate.startAngle + i * arc;
		  ctx.fillStyle = turnplate.colors[i];
		  ctx.beginPath();
		  //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线（用于创建圆或部分圆）    
		  ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);    
		  ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
		  ctx.stroke();  
		  ctx.fill();
		  //锁画布(为了保存之前的画布状态)
		  ctx.save();   
		  
		  //----绘制奖品开始----
		  ctx.fillStyle = "#E5302F";
		  var text = turnplate.restaraunts[i];
		  var line_height = 17;
		  //translate方法重新映射画布上的 (0,0) 位置
		  ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);
		  
		  //rotate方法旋转当前的绘图
		  ctx.rotate(angle + arc / 2 + Math.PI / 2);
		  
		  /** 下面代码根据奖品类型、奖品名称长度渲染不同效果，如字体、颜色、图片效果。(具体根据实际情况改变) **/
		  if(text.indexOf("M")>0){//流量包
			  var texts = text.split("M");
			  for(var j = 0; j<texts.length; j++){
				  ctx.font = j == 0?'bold 20px Microsoft YaHei':'16px Microsoft YaHei';
				  if(j == 0){
					  ctx.fillText(texts[j]+"M", -ctx.measureText(texts[j]+"M").width / 2, j * line_height);
				  }else{
					  ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
				  }
			  }
		  }else if(text.indexOf("M") == -1 && text.length>6){//奖品名称长度超过一定范围 
			  text = text.substring(0,6)+"||"+text.substring(6);
			  var texts = text.split("||");
			  for(var j = 0; j<texts.length; j++){
				  ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
			  }
		  }else{
			  //在画布上绘制填色的文本。文本的默认颜色是黑色
			  //measureText()方法返回包含一个对象，该对象包含以像素计的指定字体宽度
			  ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
		  }
		  
		  //添加对应图标
		  if(text.indexOf("闪币")>0){
			  var img= document.getElementById("shan-img");
			  img.onload=function(){  
				  ctx.drawImage(img,-15,10);      
			  }; 
			  ctx.drawImage(img,-15,10);  
		  }else if(text.indexOf("谢谢参与")>=0){
			  var img= document.getElementById("sorry-img");
			  img.onload=function(){  
				  ctx.drawImage(img,-15,10);      
			  };  
			  ctx.drawImage(img,-15,10);  
		  }
		  //把当前画布返回（调整）到上一个save()状态之前 
		  ctx.restore();
		  //----绘制奖品结束----
	  }     
  } 
}

</script>
<body>
    <header>
        <img src="../wx/choujianng/award/img/banner3.png" alt="">
    </header>

    <section>
        <div class="title">Còn: <span class="nums"></span> lần</div>

        <!-- 抽奖圆盘 -->
        <div class="round">
            <div class="roundBody">
	            	<div class="turnplate" style="background-image:url(../wx/choujianng/images/turnplate-bg.png);background-size:100% 100%;position: relative;">
					<canvas class="item" id="wheelcanvas" width="422px" height="422px"></canvas>
					<img class="pointer" src="../wx/choujianng/images/turnplate-pointer2.png" style="width: 35%;position: absolute;top: 21%;left: 33%;"/>
			    </div>
            </div>
            <div class="box">
                <p>Voucher</p>
            </div>
        </div>

        <!-- 活动规则 -->
        <div class="ruler bottom">
            <div class="ruleTop bTop">
                <p>Quy tắc chương trình</p>
            </div>
            <div class="ruleContent bContent">
                <p>1.在活动期间完成一次签到可获得一次抽奖机会，点击抽奖界面进行抽奖。</p>
                <p>2.优惠券使用时间截止2018年8月31日。</p>
                <p>3.实物奖励在获奖后7个工作日内配送至获奖店铺。</p>
                <span id="sc_wi"></span>
            </div>
        </div>

        <!-- 中奖名单 -->
        <div class="getAward bottom">
            <div class="getAwardTop bTop">
                <p>Danh sách trúng thường</p>
            </div>
            <div class="getAwardContent bContent">
                <div class="swiper">

                </div>
            </div>
        </div>
    </section>
    
    <!-- 抽奖窗口 -->
    <article class="article">
        <div class="awardBox">

            <div class="box1">
                <p>Chức mừng bạn</p>
                <p>nhận được quà <span class="awards">  </span></p>
            </div>

            <div class="box2">
                <p class="awards">  </p>
            </div>

            <div class="box3">
                Phần thường của tôi
            </div>

            <div class="box4">
                <p>Phần thường của tôi</p>
            </div>
        </div>

        <div class="line"></div>

        <div class="close"></div>
    </article>
</body>
<script>
	
    var URL = ''; // 请求地址

    {
        // 点击我的奖品显示
        $('.box').on('click',function(){
           // $('.article').show();
            $('.article').hide();
           window.location.href = "../marketingController/myAward?party_id=" + #(PARTY_ID) + "&user_id="+#(USER.ID);
        })

        // 点击关闭我的奖品
        $('.close').on('click',function(){
            $('.article').hide();
        })

        // 点击跳转页面
        $('.box3').on('click',function(){
            $('.article').hide();
           window.location.href = "../marketingController/myAward?party_id=" + #(PARTY_ID) + "&user_id="+#(USER.ID);
        })

    }

    {
        // 自适应高度设置
        var header = $('header')
        var screenHeight = header.width();
        screenHeight = screenHeight * 352 / 750;
        header.height(screenHeight);
        
        var canvasW = $('.roundBody').width();
        var canvasH = $('.roundBody').height();
        $('#wheelcanvas').width(canvasW);
        $('#wheelcanvas').height(canvasH);
    }

    {
        // 获取抽奖次数，中奖名单

        // $.ajax({
        //     type: 'get',
        //     url: URL,
        //     success: function(res){
        //         let nums = res.num; // 抽奖次数
        //         let prizeArray = res.array; // 中奖名单

        //         getData(nums, prizeArray);
        //     }
        // })

        
        var array = [
            { id: '133****9898', prize: '神秘礼物一份' },
            { id: '135****5431', prize: '一元喵卷' },
            { id: '186****2546', prize: '一元喵卷' },
            { id: '189****9685', prize: '八元喵卷' },
            { id: '151****9312', prize: '一元喵卷' },
            { id: '139****9924', prize: '八元喵卷' },
            { id: '188****5898', prize: '神秘礼物一份' },
            { id: '189****3576', prize: '神秘礼物一份' },
            { id: '186****6655', prize: '一元喵卷' }
        ];

        getData(nums, array);
    }


    // 抽奖次数，中奖名单赋值
    function getData(num, array){
        $('.nums').text(num);
        for(var i = 0; i < array.length; i++){
            var id = array[i].id;
            var prize = array[i].prize;
            var p = '<p>Chức mừng bạn<span>'+ id + '</span>nhận được quà<span>' + prize + '</span></p>';
            $('.getAwardContent .swiper').append(p);
        }
    }

    // 中奖轮播
    function autoScroll(){ 	
        $('.swiper').animate({ 
            marginTop : "-2.2rem" 
        },500,function(){ 
            $(this).css({marginTop : "0px"}).find("p:first").appendTo(this); 
        }) 
    } 
 var w= window.screen.availWidth;
	$("#sc_wi").text(w);
    // 轮播启动
    $(function(){ 
        setInterval('autoScroll()',3000);
    })
    
</script>
</html>
   #end
#@layout()